package com.googlecode.geopointer;

import java.util.Locale;
import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class TargetDialog extends Dialog implements OnClickListener{
	private Location mTarget=null;
	private EditText mLatDegree;
	private EditText mLatMin;
	private EditText mLatSec;
	private EditText mLonDegree;
	private EditText mLonMin;
	private EditText mLonSec;
	private RadioButton mLatNorth;
	private RadioButton mLonWest;
	private RadioButton mLatSouth;
	private RadioButton mLonEast;
	public TargetDialog(Context context) {
		super(context);		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.target_dialog);
		mLatDegree = (EditText) findViewById(R.id.latGrad);
		mLatMin = (EditText) findViewById(R.id.latMinute);
		mLatSec = (EditText) findViewById(R.id.latSecond);
		mLonDegree = (EditText) findViewById(R.id.lonGrad);
		mLonMin = (EditText) findViewById(R.id.lonMinute);
		mLonSec = (EditText) findViewById(R.id.lonSecond);
		mLatNorth = (RadioButton) findViewById(R.id.north);
		mLonWest = (RadioButton) findViewById(R.id.west);
		mLatSouth = (RadioButton) findViewById(R.id.south);
		mLonEast = (RadioButton) findViewById(R.id.east);
		Button b = (Button) findViewById(R.id.ok);
		b.setOnClickListener(this);
		b = (Button) findViewById(R.id.current);
		b.setOnClickListener(this);
		b = (Button) findViewById(R.id.delete);
		b.setOnClickListener(this);
		}
	@Override
	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.ok:
			double lat,lon;
			String tmp;
			try{
				double deg = Double.valueOf(mLatDegree.getText().toString());
				double min = Double.valueOf(mLatMin.getText().toString());
				tmp = mLatSec.getText().toString();
				tmp = tmp.replace(',', '.');
				double sec = Double.valueOf(tmp);
				// bug at covert ??
				if(sec>59)
					sec=59;
				tmp = String.format(Locale.US,"%1.0f:%1.0f:%f", deg,min,sec);
				lat = Location.convert(tmp);
				if(!mLatNorth.isChecked())
					lat=-lat;
				deg = Double.valueOf(mLonDegree.getText().toString());
				min = Double.valueOf(mLonMin.getText().toString());
				tmp = mLonSec.getText().toString();
				tmp = tmp.replace(',', '.');
				sec = Double.valueOf(tmp);
				if(sec>59)
					sec=59;
				tmp = String.format(Locale.US,"%1.0f:%1.0f:%f", deg,min,sec);
//				tmp = String.format(Locale.US,"%03.0f:%02.0f:%02.3f", deg,min,sec);
				lon = Location.convert(tmp);
				if(mLonWest.isChecked())
					lon=-lon;
				mTarget.setLatitude(lat);
				mTarget.setLongitude(lon);
				((PointerMain)getOwnerActivity()).targetChanged(mTarget);
			}catch(Exception e){				
			}		 
			break;
		case R.id.delete:
			mTarget=null;
			((PointerMain)getOwnerActivity()).targetChanged(mTarget);
			break;
		case R.id.current:
			fillData(((PointerMain)getOwnerActivity()).getCurrentLoc());
			return;
		}
		dismiss();		
	}
	@Override
	protected void onStart() {
		super.onStart();
		fillData(mTarget);
	}
	public void setTarget(Location loc){
		mTarget=loc;
	}
	public Location getTarget(){
		return mTarget;
	}
	private void fillData(Location loc){
		if(loc!=null){
			String lat = Location.convert(Math.abs(loc.getLatitude()),Location.FORMAT_SECONDS);
			String lon = Location.convert(Math.abs(loc.getLongitude()),Location.FORMAT_SECONDS);
			String latSplit[] = lat.split(":");
			String lonSplit[] = lon.split(":");
			if(latSplit.length > 2){
				mLatDegree.setText(latSplit[0]);
				mLatMin.setText(latSplit[1]);
				mLatSec.setText(latSplit[2]);
				if(loc.getLatitude()>0){
					mLatNorth.setChecked(true);
				}else{
					mLatSouth.setChecked(true);					
				}
			}
			if(lonSplit.length > 2){
				mLonDegree.setText(lonSplit[0]);
				mLonMin.setText(lonSplit[1]);
				mLonSec.setText(lonSplit[2]);
				if(loc.getLongitude()>0){
					mLonEast.setChecked(true);
				}else{
					mLonWest.setChecked(true);					
				}
			}
		}
	}
}
